package com.dongdong.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.dongdong.api.SellerApi;
import com.dongdong.api.UserApi;
import com.dongdong.token.LoginEnum;
import com.dongdong.model.TbAdmin;
import com.dongdong.model.TbSeller;
import com.dongdong.token.LoginToken;
import com.dongdong.utils.JwtUtil;
import com.dongdong.utils.Result;
import com.dongdong.utils.ResultEnum;
import com.dongdong.vo.AdminVO;
import com.dongdong.vo.SellerVO;
import com.dongdong.vo.UserVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private JwtUtil jwtUtil;

    @Autowired
    private SellerApi sellerApi;

    @Autowired
    private UserApi userApi;

    @PostMapping("/sellerLogin")
    public Result sellerLogin(@RequestParam String username,@RequestParam String password){
        if (StrUtil.isEmpty(username)||StrUtil.isEmpty(password)){
            return Result.DATA(ResultEnum.NULL);
        }

        Subject subject = SecurityUtils.getSubject();

        LoginToken token = new LoginToken(username,password, LoginEnum.SELLER);

        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            e.getStackTrace();
            return Result.DATA(ResultEnum.ERROR);
        }catch (IncorrectCredentialsException e){
            e.getStackTrace();
            return Result.DATA(ResultEnum.ERROR);
        }
//        获取用户
        TbSeller seller = (TbSeller) subject.getPrincipal();
//        判断状态
        if (Objects.equals(seller.getStatus(),"0")){
            return Result.DATA(ResultEnum.STATUS_0);
        }
        if (Objects.equals(seller.getStatus(),"2")){
            return Result.DATA(ResultEnum.STATUS_2);
        }
        if (Objects.equals(seller.getStatus(),"3")){
            return Result.DATA(ResultEnum.STATUS_3);
        }
//        脱敏
        SellerVO sellerVO = new SellerVO();
        BeanUtils.copyProperties(seller,sellerVO);

//        获取token
        String jwtToken = jwtUtil.createJwt(sellerVO.getSellerId(), JSONObject.toJSONString(sellerVO), "sellerRole");

        return Result.DATA(ResultEnum.LOGIN_SUCCESS).setData(jwtToken);

    }

    @PostMapping("/adminLogin")
    public Result adminLogin(@RequestParam String username,@RequestParam String password){
        if (StrUtil.isEmpty(username)||StrUtil.isEmpty(password)){
            return Result.DATA(ResultEnum.NULL);
        }

        Subject subject = SecurityUtils.getSubject();

        LoginToken token = new LoginToken(username,password,LoginEnum.ADMIN);

        try {
            subject.login(token);
        } catch (UnknownAccountException e) {
            e.getStackTrace();
            return Result.DATA(ResultEnum.ERROR);
        }catch (IncorrectCredentialsException e){
            e.getStackTrace();
            return Result.DATA(ResultEnum.ERROR);
        }
//        获取用户
        TbAdmin admin = (TbAdmin) subject.getPrincipal();
//        脱敏
        AdminVO adminVO = new AdminVO();
        BeanUtils.copyProperties(admin,adminVO);

//        获取token
        String jwtToken = jwtUtil.createJwt(adminVO.getId()+"", JSONObject.toJSONString(adminVO), "adminRole");

        return Result.DATA(ResultEnum.LOGIN_SUCCESS).setData(jwtToken);

    }


    @PostMapping("/sellerRegister")
    public Result sellerRegister(@RequestBody TbSeller seller){
        sellerApi.addSeller(seller);
        return Result.OK();
    }

    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody UserVO user){

        return userApi.register(user);
    }
}
